const helper = new Helper(this);
// 基础服务
const basicRequest = BasicServices.getInstance(prefix);
// 流媒体服务
const streamRequest = StreamServices.getInstance(prefix);
// 应急服务
const emergencyRequest = EmergencyServices.getInstance(prefix);
// 物联 service
const iotServiceRequest = IotServiceServices.getInstance(prefix);

const formEle = this.getElement();
const baseForm = this.getApiPage().retrievePlugin('base-forms');
const prefix = this.pageObj.getVariables().find(v => v.name === "environmentType")?.value;
const $form = this.getApiPage().getApiWidgetByAliasName('judge-content')
const timeline = this.getApiPage().getApiWidgetByAliasName('timelineVertical')?.widgetObj;
let data = $form?.widgetObj._option.dataSource
const selfObj = this.widgetObj;
const options = {}
const requestGet = (url) => {
    const headers = { "Content-Type": "application/json" }
    return fetch(url, { headers }).then(res => res.json())
};
const requestPost = (url, body) => {
    const options = {
        method: 'POST',
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(body)
    };
    return fetch(url, options).then(res => res.json())
};

const requestPerson = (eventId) => (
    requestGet(`/${prefix}SummaryAssessmentSV/emergencyRecord/recordFilterCriteria?eventId=${eventId}`).then(({ success, result }) => {
        console.log(this, { success, result });
        if (success && result && Array.isArray(result.personList)) return result.personList.map(item => ({
            name: item, value: item,
        }))
    })
)

const requestDict = (dictclassCode) => (
    requestGet(`/${prefix}Basics/basics-main/system/dictInfo/list?dictclassCode=${dictclassCode}`).then(({ success, result }) => {
        console.log(this, { success, result });
        if (success && result && Array.isArray(result.rows)) return result.rows.map(item => ({
            ...item, name: item.dictName, value: item.dataValue,
        }))
    })
)

function loadDict() {
    // 成员
    requestPerson(data.id).then((result) => {
        if (result) {
            selfObj.asyncSelectOptions('personName', result);
            options.eventTrendsType = result;
        }
    })
    // 动态
    requestDict('event_trends_type').then((result) => {
        if (result) {
            selfObj.asyncSelectOptions('trends', result);
            options.eventTrendsType = result;
        }
    })
    // 所有时间
    requestDict('event_time_scope').then((result) => {
        if (result) {
            selfObj.asyncSelectOptions('timeScope', result);
            options.eventTimeScope = result;
        }
    })
    // 阶段
    requestDict('event_stage_type').then((result) => {
        if (result) {
            let eventStageType = result.reduce((def, item) => {
                if (['处置中', '未处置', '已处置', '已接警', '未接警'].includes(item.name)) {
                    switch (item.name) {
                        case '处置中': item.name = item.dictName = '演练中'; break;
                        case '未处置': item.name = item.dictName = '未演练'; break;
                        case '已处置': item.name = item.dictName = '已演练'; break;
                        case '未接警': item.name = item.dictName = '未编制'; break;
                        case '已接警': item.name = item.dictName = '已编制'; break;
                    }
                    def.push(item)
                } else {
                    if (item.name.indexOf('接警') === -1 && item.name.indexOf('研判') === -1) {
                        def.push(item)
                    }
                }
                return def
            }, []);

            eventStageType = eventStageType.sort((prev, next) => prev.dataValue - next.dataValue);
            selfObj.asyncSelectOptions('stage', eventStageType)
            options.eventStageType = eventStageType;
        }
    })
}

// 加载数据字典
loadDict();

$(formEle).on('click', '.from-button', () => {
    const $form = this.getApiPage().getApiWidgetByAliasName('judge-content')?.widgetObj;
    let data = $form._option.dataSource;
    // 重置表单
    baseForm.resetForm('form_record');
    // 重置表单
    selfObj?.setFormData('form_record', {});
    selfObj?.render();
    selfObj?.asyncSelectOptions('trends', options.eventTrendsType);
    selfObj?.asyncSelectOptions('timeScope', options.eventTimeScope);
    selfObj?.asyncSelectOptions('stage', options.eventStageType);

    if (data) {
        requestPost(`/${prefix}SummaryAssessmentSV/emergencyRecord/planHandleProcessDetailByReq`, { eventId: data.id, queryType: '1' }).then(({ result, success }) => {
            if (success && Array.isArray(result)) {
                timeline.dataSource = result;
                timeline.render();
            }
            console.log('事件记录： =》 ', { result, success })
        })
    }
})


